package xuhaijia.lanou3g.autohome.find;
//                .-~~~~~~~~~-._       _.-~~~~~~~~~-.
//            __.'              ~.   .~              `.__
//          .'//                  \./                   \\`.
//        .'//            为什么坚持  想一想当初             \\`.
//      .'// .-~"""""""~~~~-._     |     _,-~~~~"""""""~-.  \\`.
//    .'//.-"                 `-.  |  .-'                 "-. \\`.
//  .'//______.============-..   \ | /   ..-============._______\\`.
//.'//____________________________\|/_____________________________\\`.
// 
//                             

import android.view.View;
import android.webkit.WebChromeClient;
import android.webkit.WebSettings;
import android.webkit.WebView;
import android.webkit.WebViewClient;
import android.widget.ProgressBar;
import android.widget.TextView;

import java.io.UnsupportedEncodingException;
import java.net.URLDecoder;

import xuhaijia.lanou3g.autohome.R;
import xuhaijia.lanou3g.autohome.base.BaseActivity;
import xuhaijia.lanou3g.autohome.base.URLValues;

/**
 * Created by 7 on 16/8/30.
 */
public class MoreActActivity extends BaseActivity implements View.OnClickListener {

    private WebView webView;
    private TextView closeTv;
    private TextView titleTv;
    private ProgressBar pb;

    @Override
    protected int setLayout() {
        return R.layout.activity_webview;
    }

    @Override
    protected void initView() {
        webView = bindView(R.id.activity_webview_wv);
        closeTv = bindView(R.id.activity_webview_close);
        closeTv.setOnClickListener(this);
        titleTv = bindView(R.id.activity_webview_title);

        pb = bindView(R.id.activity_webview_pb);
        pb.setVisibility(View.VISIBLE);
    }

    @Override
    protected void initData() {
        webView.loadUrl(URLValues.MORE_ACT_URL);
        WebSettings webSettings = webView.getSettings();
        webSettings.setJavaScriptEnabled(true);
        webView.setWebViewClient(new WebViewClient() {
            @Override
            public void onLoadResource(WebView view, String url) {
                if (url.startsWith("autohome")) {
                    url = url.replace("autohome://insidebrowser?url=", "");
//                    url = url.replace("%3a", ":");
//                    url = url.replace("%2f", "/");
//                    url = url.replace("%3f", "?");
//                    url = url.replace("%3d", "&");
                    try {
                        //编码
                        url = URLDecoder.decode(url ,"utf-8");
                    } catch (UnsupportedEncodingException e) {
                        e.printStackTrace();
                    }
                    webView.loadUrl(url);
                } else {
                    super.onLoadResource(view, url);
                }
            }

        });
        WebChromeClient webChromeClient = new WebChromeClient() {
            @Override
            public void onReceivedTitle(WebView view, String title) {
                super.onReceivedTitle(view, title);
                titleTv.setText(title);
            }

            @Override
            public void onProgressChanged(WebView view, int newProgress) {
                super.onProgressChanged(view, newProgress);
                pb.setProgress(newProgress);
                if (newProgress == 100) {
                    pb.setVisibility(View.GONE);
                }

            }
        };

        webView.setWebChromeClient(webChromeClient);

    }

    @Override
    public void onClick(View v) {
        switch (v.getId()) {
            case R.id.activity_webview_close:
                finish();
                break;
        }
    }
}
